package module{
	import com.greensock.TweenLite;
	
	import component.Image;
	
	import flash.display.BitmapData;
	
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	
	public class Ball extends DisplayObject3D{
		
		public static var instance:Ball = new Ball();
		
		private var sphere:Sphere;	
		private var _numberOfVerts:uint;
		public var blockList:Vector.<Plane>;
		
		public var src:String = "resource/pic%i.jpg";
		
		public var open:Boolean;
		
		public function Ball(){
			this.sphere = new Sphere(null,Global.BALL_RADIUS,Global.BALL_SEGMENTSW,Global.BALL_SEGMENTSH);
			this._numberOfVerts = sphere.geometry.vertices.length;
			this.blockList = new Vector.<Plane>;
			initBlock();
		}
		
		private function initBlock():void{
			for (var i:uint= 1; i < _numberOfVerts-1; i++){
				var url:String = src.replace("%i",i);
				var asset:BitmapData = Image.getDataFromPool(url);
				if(asset == null)asset = Image.getDataFromPool(src.replace("%i",int(Math.random()*11+1)));
				var materia:BitmapMaterial = new BitmapMaterial(asset);
				materia.smooth = true;
				var plane:Plane = new Plane(materia,asset.width,asset.height);
				plane.x = sphere.geometry.vertices[i].x;
				plane.y = sphere.geometry.vertices[i].y;
				plane.z = sphere.geometry.vertices[i].z;
				
				if (Math.abs(plane.z) < 0.00001){
					if (plane.x > 0){
						plane.rotationX=Math.atan2(plane.y, plane.x) * 180 / Math.PI;
						plane.rotationY=-90;
					}else{
						plane.rotationX=180 - Math.atan2(plane.y, plane.x) * 180 / Math.PI;
						plane.rotationY=90;
					}
				}else if (plane.z > 0){
					plane.rotationX=Math.atan2(plane.y,  Math.sqrt(plane.z*plane.z+plane.x*plane.x)) * 180 / Math.PI;
					plane.rotationY=180 + Math.atan2(plane.x, plane.z) * 180 / Math.PI;
				}else	{
					plane.rotationX=180 - Math.atan2(plane.y, -Math.sqrt(plane.z*plane.z+plane.x*plane.x)) * 180 / Math.PI;
					plane.rotationY=180 + Math.atan2(plane.x, plane.z) * 180 / Math.PI;
				}
				plane.rotationX;
				plane.rotationY;
				plane.rotationZ;
				blockList.push(plane);
				plane.material.doubleSided = true;
				this.addChild(plane);
			}
		}
	
	}
}